﻿namespace Commission.Migrations
{
    using Commission.Models;
    using Commission.DAL;
    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;
    using Commission.Helpers;

    internal sealed class Configuration : DbMigrationsConfiguration<SchoolContext>
    {
        const string BP = "BP";
        const string NP = "NP";
        const string VP = "VP";
        const string AP = "AP";
        const string VP150 = "VP150";
        const string AP700 = "AP700";
        const string F100 = "100F";
        const string F300 = "300F";
        const string F1000 = "1000F";
        const string F300_1 = "300F1"; // goi 300 va 1 diem can chan
        const int validDays = 61;

        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
        }

        protected override void Seed(SchoolContext context)
        {
            MyInit(context);
        }

        private void MyInit(SchoolContext context)
        {
            var customertypes = new List<CustomerType>
            {
                new CustomerType { 
                    TypeName = BP, FullTypeName = "Cộng tác viên",
                    Point = 0, 
                    AttendantFee = 0, 
                    MaxDailyProfit = 0, 
                    MaxSponsorAmount = 0,
                    ValidDays = 3650, 
                    IsOnline = false, IsUsed = true,
                },
                new CustomerType { 
                    TypeName = NP, FullTypeName = "Gói 100 (NORMAL PACKAGE)",
                    Point = 1,
                    AttendantFee = 100 * Const.Rate, 
                    MaxDailyProfit = 180 * Const.Rate, 
                    MaxSponsorAmount = 35* Const.Rate,
                    ValidDays = validDays, 
                    IsOnline = false, IsUsed = true,
                },
                new CustomerType { 
                    TypeName = VP150, FullTypeName = "Gói 150 (VIP PACKAGE)",
                    Point = new decimal(1.5), 
                    AttendantFee = 150 * Const.Rate, 
                    MaxDailyProfit = 630 * Const.Rate, 
                    MaxSponsorAmount = 55 * Const.Rate,
                    ValidDays = validDays, 
                    IsOnline = false, IsUsed = true,
                },
                new CustomerType { 
                    TypeName = VP, FullTypeName = "Gói 300 (VIP PACKAGE)",
                    Point = 3, 
                    AttendantFee = 300 * Const.Rate, 
                    MaxDailyProfit = 630 * Const.Rate, 
                    MaxSponsorAmount = 110 * Const.Rate,
                    ValidDays = validDays, IsOnline = false, IsUsed = true,
                },
                new CustomerType { 
                    TypeName = AP700, FullTypeName = "Gói 700 (ADVANCED PACKAGE)",
                    Point = 7, 
                    AttendantFee = 700 * Const.Rate, 
                    MaxDailyProfit = 99999 * Const.Rate, 
                    MaxSponsorAmount = 224 * Const.Rate,
                    ValidDays = validDays, IsOnline = true, IsUsed = true,
                },
                new CustomerType { 
                    TypeName = AP, FullTypeName = "Gói 1000 (ADVANCED PACKAGE)",
                    Point = 10, 
                    AttendantFee = 1000 * Const.Rate, 
                    MaxDailyProfit = 99999 * Const.Rate, 
                    MaxSponsorAmount = 320 * Const.Rate,
                    ValidDays = validDays, 
                    IsOnline = true, IsUsed = true,
                },
                new CustomerType { 
                    TypeName = F100, FullTypeName = "Gói 100F",
                    Point = 0, 
                    AttendantFee = 0 * Const.Rate, 
                    MaxDailyProfit = 180 * Const.Rate, 
                    MaxSponsorAmount = 0 * Const.Rate,
                    ValidDays = validDays, 
                    IsOnline = false, IsUsed = true,
                },
                new CustomerType { 
                    TypeName = F300, FullTypeName = "Gói 300F",
                    Point = 0, 
                    AttendantFee = 0 * Const.Rate, 
                    MaxDailyProfit = 630 * Const.Rate, 
                    MaxSponsorAmount = 0 * Const.Rate,
                    ValidDays = validDays, 
                    IsOnline = false, IsUsed = true,
                },
                new CustomerType { 
                    TypeName = F1000, FullTypeName = "Gói 1000F",
                    Point = 0, 
                    AttendantFee = 0 * Const.Rate, 
                    MaxDailyProfit = 99999  * Const.Rate, 
                    MaxSponsorAmount = 0 * Const.Rate,
                    ValidDays = validDays, 
                    IsOnline = true, IsUsed = true,
                },
                new CustomerType { 
                    TypeName = F300_1, FullTypeName = "Gói 300F1",
                    Point = 1, 
                    AttendantFee = 100 * Const.Rate, 
                    MaxDailyProfit = 630 * Const.Rate, 
                    MaxSponsorAmount = 0 * Const.Rate,
                    ValidDays = validDays, 
                    IsOnline = false, IsUsed = true,
                },
            };
            customertypes.ForEach(s => context.CustomerTypes.AddOrUpdate(p => p.TypeName, s));
            context.SaveChanges();

            InsertSponsorCommission(context, customertypes);

            InsertPointCommission(context, customertypes);

            var units = new List<Unit>
            {
                new Unit { UnitName = "Hộp" },
                new Unit { UnitName = "Bộ" },
                new Unit { UnitName = "Chai" },
            };
            units.ForEach(
                s => context.Units.AddOrUpdate(p => p.UnitName, s));
            context.SaveChanges();

            var uniLevels = new List<UniLevel>
            {
                new UniLevel { Level = 1, 
                    Percent = new decimal(6), MatchingPercent = new decimal(20) },
                new UniLevel { Level = 2, 
                    Percent = new decimal(5), MatchingPercent = new decimal(20) },
                new UniLevel { Level = 3, 
                    Percent = new decimal(5), MatchingPercent = new decimal(20) },
                new UniLevel { Level = 4,
                    Percent = new decimal(4), MatchingPercent = new decimal(20) },
                new UniLevel { Level = 5, 
                    Percent = new decimal(4), MatchingPercent = new decimal(20) },
                new UniLevel { Level = 6,
                    Percent = new decimal(4), MatchingPercent = new decimal(20) },
                new UniLevel { Level = 7,
                    Percent = new decimal(3), MatchingPercent = new decimal(20) },
                new UniLevel { Level = 8,
                    Percent = new decimal(3), MatchingPercent = new decimal(20) },
                new UniLevel { Level = 9,
                    Percent = new decimal(3), MatchingPercent = new decimal(20) },
                new UniLevel { Level = 10, 
                    Percent = new decimal(3), MatchingPercent = new decimal(20) },
            };
            uniLevels.ForEach(
                s => context.UniLevels.AddOrUpdate(p => p.Level, s));
            context.SaveChanges();

            InsertCustomer(context);
        }

        private void InsertSponsorCommission(SchoolContext context, List<CustomerType> customertypes)
        {
            var commissions = new List<SponsorCommission>
            {
                // NP
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == NP).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == NP).ID,
                    Amount = 35 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == NP).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == VP150).ID,
                    Amount = new decimal(27.5)  * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == NP).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == VP).ID,
                    Amount = 55 * Const.Rate, 
                    UpgradeDays = 30
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == NP).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == AP700).ID,
                    Amount = 70 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == NP).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == AP).ID,
                    Amount = 100 * Const.Rate, 
                    UpgradeDays = 90
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == NP).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F100).ID,
                    Amount = 0 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == NP).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F300).ID,
                    Amount = 0 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == NP).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F1000).ID,
                    Amount = 0 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == NP).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F300_1).ID,
                    Amount = 35 * Const.Rate, 
                    UpgradeDays = 30
                },

                // 100F
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == F100).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == NP).ID,
                    Amount = 35 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == F100).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == VP150).ID,
                    Amount = new decimal(27.5) * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == F100).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == VP).ID,
                    Amount = 55 * Const.Rate, 
                    UpgradeDays = 30
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == F100).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == AP700).ID,
                    Amount = 70 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == F100).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == AP).ID,
                    Amount = 100 * Const.Rate, 
                    UpgradeDays = 90
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == F100).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F100).ID,
                    Amount = 0 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == F100).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F300).ID,
                    Amount = 0 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == F100).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F1000).ID,
                    Amount = 0 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == F100).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F300_1).ID,
                    Amount = 35 * Const.Rate, 
                    UpgradeDays = 30
                },

                // VP150
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == VP150).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == NP).ID,
                    Amount = 35 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == VP150).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == VP150).ID,
                    Amount = 55 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == VP150).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == VP).ID,
                    Amount = 110 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == VP150).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == AP700).ID,
                    Amount = 112 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == VP150).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == AP).ID,
                    Amount = 160 * Const.Rate, 
                    UpgradeDays = 60
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == VP150).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F100).ID,
                    Amount = 0 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == VP150).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F300).ID,
                    Amount = 0 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == VP150).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F1000).ID,
                    Amount = 0 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == VP150).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F300_1).ID,
                    Amount = 35 * Const.Rate, 
                    UpgradeDays = 0
                },

                // VP300
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == VP).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == NP).ID,
                    Amount = 35 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == VP).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == VP150).ID,
                    Amount = 55 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == VP).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == VP).ID,
                    Amount = 110 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == VP).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == AP700).ID,
                    Amount = 112 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == VP).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == AP).ID,
                    Amount = 160 * Const.Rate, 
                    UpgradeDays = 60
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == VP).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F100).ID,
                    Amount = 0 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == VP).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F300).ID,
                    Amount = 0 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == VP).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F1000).ID,
                    Amount = 0 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == VP).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F300_1).ID,
                    Amount = 35 * Const.Rate, 
                    UpgradeDays = 0
                },

                // 300F
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == F300).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == NP).ID,
                    Amount = 35 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == F300).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == VP150).ID,
                    Amount = 55 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == F300).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == VP).ID,
                    Amount = 110 * Const.Rate,
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == F300).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == AP700).ID,
                    Amount = 112 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == F300).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == AP).ID,
                    Amount = 160 * Const.Rate,
                    UpgradeDays = 60
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == F300).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F100).ID,
                    Amount = 0 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == F300).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F300).ID,
                    Amount = 0 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == F300).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F1000).ID,
                    Amount = 0 * Const.Rate,
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == F300).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F300_1).ID,
                    Amount = 35 * Const.Rate, 
                    UpgradeDays = 0
                },

                // AP700
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == AP700).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == NP).ID,
                    Amount = 35 * Const.Rate,
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == AP700).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == VP150).ID,
                    Amount = 55 * Const.Rate,
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == AP700).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == VP).ID,
                    Amount = 110 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == AP700).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == AP700).ID,
                    Amount = 224 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == AP700).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == AP).ID,
                    Amount = 320 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == AP700).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F100).ID,
                    Amount = 0 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == AP700).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F300).ID,
                    Amount = 0 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == AP700).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F1000).ID,
                    Amount = 0 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == AP700).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F300_1).ID,
                    Amount = 35 * Const.Rate, 
                    UpgradeDays = 0
                },

                // AP1000
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == AP).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == NP).ID,
                    Amount = 35 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == AP).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == VP150).ID,
                    Amount = 55 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == AP).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == VP).ID,
                    Amount = 110 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == AP).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == AP700).ID,
                    Amount = 224 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == AP).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == AP).ID,
                    Amount = 320 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == AP).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F100).ID,
                    Amount = 0 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == AP).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F300).ID,
                    Amount = 0 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == AP).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F1000).ID,
                    Amount = 0 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == AP).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F300_1).ID,
                    Amount = 35 * Const.Rate, 
                    UpgradeDays = 0
                },

                // F1000
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == F1000).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == NP).ID,
                    Amount = 35 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == F1000).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == VP150).ID,
                    Amount = 55 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == F1000).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == VP).ID,
                    Amount = 110 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == F1000).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == AP700).ID,
                    Amount = 224 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == F1000).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == AP).ID,
                    Amount = 320 * Const.Rate,
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == F1000).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F100).ID,
                    Amount = 0 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == F1000).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F300).ID,
                    Amount = 0 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == F1000).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F1000).ID,
                    Amount = 0 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == F1000).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F300_1).ID,
                    Amount = 35 * Const.Rate, 
                    UpgradeDays = 0
                },

                // 300F1
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == F300_1).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == NP).ID,
                    Amount = 35 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == F300_1).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == VP150).ID,
                    Amount = 55 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == F300_1).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == VP).ID,
                    Amount = 110 * Const.Rate,
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == F300_1).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == AP700).ID,
                    Amount = 112 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == F300_1).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == AP).ID,
                    Amount = 160 * Const.Rate,
                    UpgradeDays = 60
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == F300_1).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F100).ID,
                    Amount = 0 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == F300_1).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F300).ID,
                    Amount = 0 * Const.Rate, 
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == F300_1).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F1000).ID,
                    Amount = 0 * Const.Rate,
                    UpgradeDays = 0
                },
                new SponsorCommission { 
                    SponsorCustomerTypeID = customertypes.Single(c => c.TypeName == F300_1).ID,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F300_1).ID,
                    Amount = 35 * Const.Rate, 
                    UpgradeDays = 0
                },
            };
            commissions.ForEach(s => context.SponsorCommissions.AddOrUpdate(
                p => new { p.SponsorCustomerTypeID, p.CustomerTypeID }, s));
            context.SaveChanges();
        }

        private void InsertPointCommission(SchoolContext context, List<CustomerType> customertypes)
        {
            var pointCommissions = new List<PointCommission>
            {
                new PointCommission { MinPoint = 1, MaxPoint = 99999, Amount = 0 * Const.Rate, 
                    CustomerTypeID = customertypes.Single(c => c.TypeName == BP).ID },

                new PointCommission { MinPoint = 1, MaxPoint = 4, Amount = 15 * Const.Rate, 
                    CustomerTypeID = customertypes.Single(c => c.TypeName == NP).ID },
                new PointCommission { MinPoint = 5, MaxPoint = 40, Amount = 3 * Const.Rate,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == NP).ID },

                new PointCommission { MinPoint = 1, MaxPoint = 8, Amount = 15 * Const.Rate,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == VP150).ID },
                new PointCommission { MinPoint = 9, MaxPoint = 170, Amount = 3 * Const.Rate,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == VP150).ID },
                
                new PointCommission { MinPoint = 1, MaxPoint = 8, Amount = 15 * Const.Rate,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == VP).ID },
                new PointCommission { MinPoint = 9, MaxPoint = 170, Amount = 3 * Const.Rate,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == VP).ID },

                new PointCommission { MinPoint = 1, MaxPoint = 16, Amount = 15 * Const.Rate,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == AP700).ID },
                new PointCommission { MinPoint = 17, MaxPoint = 99999, Amount = 3 * Const.Rate,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == AP700).ID },
                
                new PointCommission { MinPoint = 1, MaxPoint = 16, Amount = 15 * Const.Rate,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == AP).ID },
                new PointCommission { MinPoint = 17, MaxPoint = 99999, Amount = 3 * Const.Rate,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == AP).ID },

                new PointCommission { MinPoint = 1, MaxPoint = 4, Amount = 15 * Const.Rate, 
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F100).ID },
                new PointCommission { MinPoint = 5, MaxPoint = 40, Amount = 3 * Const.Rate,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F100).ID },

                new PointCommission { MinPoint = 1, MaxPoint = 8, Amount = 15 * Const.Rate,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F300).ID },
                new PointCommission { MinPoint = 9, MaxPoint = 170, Amount = 3 * Const.Rate,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F300).ID },

                new PointCommission { MinPoint = 1, MaxPoint = 16, Amount = 15 * Const.Rate,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F1000).ID },
                new PointCommission { MinPoint = 17, MaxPoint = 99999, Amount = 3 * Const.Rate,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F1000).ID },

                new PointCommission { MinPoint = 1, MaxPoint = 8, Amount = 15 * Const.Rate,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F300_1).ID },
                new PointCommission { MinPoint = 9, MaxPoint = 170, Amount = 3 * Const.Rate,
                    CustomerTypeID = customertypes.Single(c => c.TypeName == F300_1).ID },
            };
            pointCommissions.ForEach(
                s => context.PointCommissions.AddOrUpdate(p => new { p.CustomerTypeID, p.MaxPoint }, s));
            context.SaveChanges();
        }

        private string CreateRandomIDNumber()
        {
            var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            var random = new Random();
            var result = new string(
                Enumerable.Repeat(chars, 9)
                          .Select(s => s[random.Next(s.Length)])
                          .ToArray());
            return result;
        }

        void InsertCustomer(SchoolContext context)
        {
            const string Admin01 = "Admin01";
            const string HueHung = "Huệ Hùng";
            const string GiaToan = "Gia Toàn";
            const string GiaTuan = "Gia Tuấn";

            var now = DateTime.Now;
            var validDays = 61;

            var customers = new List<Customer>
            {
                new Customer { 
                    Name = Admin01,
                    UserName = "Admin01",
                    IDCard = CreateRandomIDNumber(),
                    BirthDay = DateTime.Parse("1968-03-31"), 
                    CustomerTypeID = (int)CustomerKind.BP, 
                    Position = 0,
                    AttendantDate = now, UpgradeDate = now, ValidDate = now.AddDays(validDays)
                },
                new Customer { 
                    Name = HueHung,
                    UserName = "HueHung",
                    IDCard = CreateRandomIDNumber(),
                    BirthDay = DateTime.Parse("1979-03-31"), 
                    CustomerTypeID = (int)CustomerKind.AP, 
                    Position = 0, AttendantDate = now, UpgradeDate = now, ValidDate = now.AddDays(60)
                },
            };
            customers.ForEach(
                s => context.Customers.AddOrUpdate(p =>
                    new { p.UserName }, s));
            context.SaveChanges();

            /*
            var accountHistories = new List<TransactionHistory>
            {
                new TransactionHistory { 
                    CustomerID = context.Customers.Single(c => c.Name == HueHung).CustomerID,
                    AccountType = Models.AccountType.Pairing,
                    AccountDate = DateTime.Now,
                    Point = 10,
                    Amount = new decimal(62.4),
                    ProductAmount = new decimal(15.6),
                    TransactionStatus = Models.TransactionStatus.Finish,
                    CreatedUserId = context.Customers.Single(c => c.Name == HueHung).CustomerID,
                    CreatedAt = DateTime.Now,
                    UpdatedUserId = context.Customers.Single(c => c.Name == HueHung).CustomerID,
                    UpdatedAt = DateTime.Now
                },
                new TransactionHistory { 
                    CustomerID = context.Customers.Single(c => c.Name == HueHung).CustomerID,
                    AccountType = Models.AccountType.Pairing,
                    AccountDate = DateTime.Now,
                    Point = 3,
                    Amount = 36,
                    ProductAmount = 9,
                    TransactionStatus = Models.TransactionStatus.Finish,
                    CreatedUserId = context.Customers.Single(c => c.Name == HueHung).CustomerID,
                    CreatedAt = DateTime.Now,
                    UpdatedUserId = context.Customers.Single(c => c.Name == HueHung).CustomerID,
                    UpdatedAt = DateTime.Now
                },
                new TransactionHistory { 
                    CustomerID = context.Customers.Single(c => c.Name == HueHung).CustomerID,
                    AccountType = Models.AccountType.Pairing,
                    AccountDate = DateTime.Now,
                    Point = 6,
                    Amount = new decimal(52.8),
                    ProductAmount = new decimal(13.2),
                    TransactionStatus = Models.TransactionStatus.Finish,
                    CreatedUserId = context.Customers.Single(c => c.Name == HueHung).CustomerID,
                    CreatedAt = DateTime.Now,
                    UpdatedUserId = context.Customers.Single(c => c.Name == HueHung).CustomerID,
                    UpdatedAt = DateTime.Now
                }
            };
            accountHistories.ForEach(s => context.TransactionHistories.AddOrUpdate(
                p => new { p.CustomerID, p.Point, p.Amount, p.AccountType }, s));
            context.SaveChanges();
             */

            customers = new List<Customer>
            {
                new Customer { Name = GiaToan,
                    UserName = "GiaToan",
                    BirthDay = DateTime.Parse("2008-05-18"),
                    IDCard = CreateRandomIDNumber(),
                    CustomerTypeID = (int)CustomerKind.VP, 
                    Position = 1, 
                    AttendantDate = now, UpgradeDate = now, ValidDate = now.AddDays(60),
                    ParentCustomerID = context.Customers.Single(c => c.Name == HueHung).CustomerID,
                    SponsorCustomerID = context.Customers.Single(c => c.Name == HueHung).CustomerID
                },
                new Customer { Name = GiaTuan,
                    UserName = "GiaTuan",
                    BirthDay = DateTime.Parse("2012-04-14"),
                    IDCard = CreateRandomIDNumber(),
                    CustomerTypeID = (int)CustomerKind.VP, 
                    Position = 2, 
                    AttendantDate = now, UpgradeDate = now, ValidDate = now.AddDays(60),
                    ParentCustomerID = context.Customers.Single(c => c.Name == HueHung).CustomerID,
                    SponsorCustomerID = context.Customers.Single(c => c.Name == HueHung).CustomerID
                }
            };
            customers.ForEach(s => context.Customers.AddOrUpdate(p => p.UserName, s));
            context.SaveChanges();

            customers = new List<Customer>
            {
                new Customer { Name = GiaToan + " 1-1",
                    UserName = "GiaToan11",
                    BirthDay = DateTime.Parse("2008-05-18"),
                    IDCard = CreateRandomIDNumber(),
                    CustomerTypeID = (int)CustomerKind.NP, 
                    Position = 1, 
                    AttendantDate = now, UpgradeDate = now, ValidDate = now.AddDays(60),
                    ParentCustomerID = context.Customers.Single(c => c.Name == GiaToan).CustomerID,
                    SponsorCustomerID = context.Customers.Single(c => c.Name == GiaToan).CustomerID
                },
                new Customer { Name = GiaToan + " 1-2",
                    UserName = "GiaToan12",
                    BirthDay = DateTime.Parse("2008-05-18"),
                    IDCard = CreateRandomIDNumber(),
                    CustomerTypeID = (int)CustomerKind.AP, 
                    Position = 2, 
                    AttendantDate = now, UpgradeDate = now, ValidDate = now.AddDays(60),
                    ParentCustomerID = context.Customers.Single(c => c.Name == GiaToan).CustomerID,
                    SponsorCustomerID = context.Customers.Single(c => c.Name == HueHung).CustomerID
                },
                new Customer { Name = "Gia Tuấn 1-1",
                    UserName = "GiaTuan11",
                    BirthDay = DateTime.Parse("2012-04-14"),
                    IDCard = CreateRandomIDNumber(),
                    CustomerTypeID = (int)CustomerKind.VP, 
                    Position = 2,
                    AttendantDate = now, UpgradeDate = now, ValidDate = now.AddDays(60),
                    ParentCustomerID = context.Customers.Single(c => c.Name == GiaTuan).CustomerID,
                    SponsorCustomerID = context.Customers.Single(c => c.Name == GiaTuan).CustomerID
                },
                new Customer { Name = "Gia Tuấn 1-2",
                    UserName = "GiaTuan12",
                    BirthDay = DateTime.Parse("2012-04-14"),
                    IDCard = CreateRandomIDNumber(),
                    CustomerTypeID = (int)CustomerKind.VP, 
                    Position = 2, 
                    AttendantDate = now, UpgradeDate = now, ValidDate = now.AddDays(60),
                    ParentCustomerID = context.Customers.Single(c => c.Name == GiaTuan).CustomerID,
                    SponsorCustomerID = context.Customers.Single(c => c.Name == GiaTuan).CustomerID
                }
            };
            customers.ForEach(s => context.Customers.AddOrUpdate(
                p => new { p.Name }, s));
            context.SaveChanges();

            customers = new List<Customer>
            {
                new Customer { Name = "Gia Toàn 1-1-1",
                    UserName = "GiaToan111",
                    BirthDay = DateTime.Parse("2008-05-18"),
                    IDCard = CreateRandomIDNumber(),
                    CustomerTypeID = 4, Position = 1, 
                    AttendantDate = now, UpgradeDate = now, ValidDate = now.AddDays(60),
                    ParentCustomerID = context.Customers.Single(c => c.Name == "Gia Toàn 1-1").CustomerID,
                    SponsorCustomerID = context.Customers.Single(c => c.Name == GiaToan).CustomerID
                },
                new Customer { Name = "Gia Toàn 1-1-2",
                    UserName = "GiaToan112",
                    BirthDay = DateTime.Parse("2008-05-18"),
                    IDCard = CreateRandomIDNumber(),
                    CustomerTypeID = (int)CustomerKind.AP, 
                    Position = 2, 
                    AttendantDate = now, UpgradeDate = now, ValidDate = now.AddDays(60),
                    ParentCustomerID = context.Customers.Single(c => c.Name == "Gia Toàn 1-1").CustomerID,
                    SponsorCustomerID = context.Customers.Single(c => c.Name == HueHung).CustomerID
                },
                new Customer { Name = "Gia Toàn 1-2-1",
                    UserName = "GiaToan121",
                    BirthDay = DateTime.Parse("2008-05-18"),
                    IDCard = CreateRandomIDNumber(),
                    CustomerTypeID = 2, Position = 1,
                    AttendantDate = now, UpgradeDate = now, ValidDate = now.AddDays(60),
                    ParentCustomerID = context.Customers.Single(c => c.Name == "Gia Toàn 1-2").CustomerID,
                    SponsorCustomerID = context.Customers.Single(c => c.Name == GiaToan).CustomerID
                },
                new Customer { Name = "Gia Toàn 1-2-2", 
                    UserName = "GiaToan122",
                    BirthDay = DateTime.Parse("2008-05-18"),
                    IDCard = CreateRandomIDNumber(),
                    CustomerTypeID = 3, Position = 2,
                    AttendantDate = now, UpgradeDate = now, ValidDate = now.AddDays(60),
                    ParentCustomerID = context.Customers.Single(c => c.Name == "Gia Toàn 1-2").CustomerID,
                    SponsorCustomerID = context.Customers.Single(c => c.Name == GiaToan).CustomerID
                },
                new Customer { Name = "Gia Tuấn 1-1-1",
                    UserName = "GiaTuan111",
                    BirthDay = DateTime.Parse("2012-04-14"),
                    IDCard = CreateRandomIDNumber(),
                    CustomerTypeID = 2, Position = 2,
                    AttendantDate = now, UpgradeDate = now, ValidDate = now.AddDays(60),
                    ParentCustomerID = context.Customers.Single(c => c.Name == "Gia Tuấn 1-1").CustomerID,
                    SponsorCustomerID = context.Customers.Single(c => c.Name == HueHung).CustomerID
                },
                new Customer { Name = "Gia Tuấn 1-1-2",
                    UserName = "GiaTuan112",
                    BirthDay = DateTime.Parse("2012-04-14"),
                    IDCard = CreateRandomIDNumber(),
                    CustomerTypeID = 4, Position = 2,
                    AttendantDate = now, UpgradeDate = now, ValidDate = now.AddDays(60),
                    ParentCustomerID = context.Customers.Single(c => c.Name == "Gia Tuấn 1-1").CustomerID,
                    SponsorCustomerID = context.Customers.Single(c => c.Name == "Gia Tuấn 1-1").CustomerID
                },
                new Customer { Name = "Gia Tuấn 1-2-1",
                    UserName = "GiaTuan121",
                    BirthDay = DateTime.Parse("2012-04-14"),
                    IDCard = CreateRandomIDNumber(),
                    CustomerTypeID = 3, 
                    Position = 2,
                    AttendantDate = now, UpgradeDate = now, ValidDate = now.AddDays(60),
                    ParentCustomerID = context.Customers.Single(c => c.Name == "Gia Tuấn 1-2").CustomerID,
                    SponsorCustomerID = context.Customers.Single(c => c.Name == HueHung).CustomerID
                },
                new Customer { Name = "Gia Tuấn 1-2-2",
                    UserName = "GiaTuan122",
                    BirthDay = DateTime.Parse("2012-04-14"),
                    IDCard = CreateRandomIDNumber(),
                    CustomerTypeID = 2, 
                    Position = 2, 
                    AttendantDate = now, UpgradeDate = now, ValidDate = now.AddDays(60),
                    ParentCustomerID = context.Customers.Single(c => c.Name == "Gia Tuấn 1-2").CustomerID,
                    SponsorCustomerID = context.Customers.Single(c => c.Name == GiaTuan).CustomerID
                }
            };
            customers.ForEach(s => context.Customers.AddOrUpdate(
                p => new { p.Name }, s));
            context.SaveChanges();
        }

        void AddOrUpdateInstructor(SchoolContext context, string courseTitle, string instructorName)
        {
            var crs = context.Courses.SingleOrDefault(c => c.Title == courseTitle);
            var inst = crs.Instructors.SingleOrDefault(i => i.LastName == instructorName);
            if (inst == null)
                crs.Instructors.Add(context.Instructors.Single(i => i.LastName == instructorName));
        }
    }
}